package com.slaffka.model.notification;

import com.slaffka.model.NotificationType;
import com.slaffka.model.user.User;
import com.slaffka.model.user.UserDTO;

import java.util.Collection;
import java.util.Collections;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: v.tsapaev
 * Date: 16.03.12
 * Time: 23:56
 * To change this template use File | Settings | File Templates.
 */
public abstract class NotificationDTO{
    
    protected Long notificationId;

    protected Collection<UserDTO> targetUsers;

    protected UserDTO owner;

    protected Date createDate;

    protected NotificationType notificationType;

    public NotificationDTO(){

    }

    public NotificationDTO(Long notificationId, Collection<UserDTO> targetUsers, NotificationType notificationType, UserDTO owner) {
        this.notificationId = notificationId;
        this.targetUsers = Collections.emptySet();
        for (UserDTO userDTO: targetUsers){
            targetUsers.add(userDTO);
        }
        this.notificationType = notificationType;
        this.owner = owner;
    }

    public Long getNotificationId() {
        return notificationId;
    }

    public void setNotificationId(Long notificationId) {
        this.notificationId = notificationId;
    }

    public Collection<UserDTO> getTargetUsers() {
        return targetUsers;
    }

    public void setTargetUsers(Collection<UserDTO> targetUsers) {
        this.targetUsers = targetUsers;
    }

    public NotificationType getNotificationType() {
        return notificationType;
    }

    public UserDTO getOwner() {
        return owner;
    }

    public void setOwner(UserDTO owner) {
        this.owner = owner;
    }

    public Date getCreateDate() {
        return createDate;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public static NotificationDTO convertToDTO(Notification notification) {
        if (notification == null){
            return null;
        }
        UserDTO owner = UserDTO.convertToDTO(notification.getOwner());

        Collection<UserDTO> targetUsers = Collections.emptySet();
        if (notification.getTargetUsers() != null){
            for (User user: notification.getTargetUsers()){
                targetUsers.add(UserDTO.convertToDTO(user));
            }
        }

        NotificationDTO notificationDTO;
        if (notification instanceof Message){
            Message message = (Message)notification;
            MessageDTO messageDTO = new MessageDTO();
            messageDTO.setText(message.getText());
            messageDTO.setTitle(message.getTitle());
            notificationDTO = messageDTO;
        } else {
            notificationDTO = new NotificationDTO() {};
        }
        
        notificationDTO.setOwner(owner);
        notificationDTO.setCreateDate(notification.getCreateDate());
        notificationDTO.setNotificationId(notificationDTO.getNotificationId());
        notificationDTO.setTargetUsers(targetUsers);
        notificationDTO.setNotificationType(notification.getNotificationType());
        
        return notificationDTO;
    }

    private void setNotificationType(NotificationType notificationType) {
        this.notificationType = notificationType;
    }
}
